package com.drink.yan.job;

import com.drink.yan.intf.define.YanActivityStatusE;
import com.drink.yan.intf.define.YanTaskStatusE;
import com.drink.yan.intf.vo.ActivityVo;
import com.drink.yan.intf.vo.TaskVo;
import com.drink.yan.svc.service.YanActivityService;
import com.drink.yan.svc.service.YanTaskService;
import com.drink.yan.web.rest.req.qry.PageActivityQry;
import com.drink.yan.web.rest.req.qry.PageTaskQry;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 定时任务，计算活动状态
 *
 * @Title:
 * @Package: com.drink.yan.job.
 * @Author: M.simple
 * @Remark: The modified content
 * @CreateDate: 2023-12-18 12:12
 * @Version: v2.0
 */
@Slf4j
@Component("YanTaskStatusJob")
public class YanTaskStatusJob {

	@Resource
	private YanTaskService yanTaskService;

	public void handle(){

		log.info("===========start YanTaskStatusJob===========");

		PageTaskQry pageTaskQry = new PageTaskQry();
		pageTaskQry.setPageNum(1);
		pageTaskQry.setPageSize(1000);
		List<String> statusList = Lists.newArrayList(YanTaskStatusE.ready.getCode(), YanTaskStatusE.started.getCode());
		pageTaskQry.setTaskStatusList(statusList);
		List<TaskVo> activityVos = yanTaskService.listTask(pageTaskQry);
		activityVos.forEach(x->{
			LocalDateTime now = LocalDateTime.now();
			if(YanTaskStatusE.ready.getCode().equals(x.getTaskStatus()) && x.getBeginTime().isBefore(now) && x.getEndTime().isAfter(now)){
				x.setTaskStatus(YanTaskStatusE.started.getCode());
				yanTaskService.saveTask(x, false);
			}else if(YanTaskStatusE.started.getCode().equals(x.getTaskStatus()) && x.getEndTime().isBefore(now)){
				x.setTaskStatus(YanTaskStatusE.ended.getCode());
				yanTaskService.saveTask(x, false);
			}
		});
	}
}
